angular.module('BaseServer')

.factory('EntityService', ['BaseFunction',
    function(BaseFunction) {
        return {
            save: function(entityName, records, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    Records: records
                };

                BaseFunction.request('/api/entity/save', false, parameter, successHandle, errHandle);
            },
            saves: function(entityRecords, successHandle, errHandle) {
                var parameter = {
                    EntityRecords: entityRecords
                };

                BaseFunction.request('/api/entity/saves', false, parameter, successHandle, errHandle);
            },

            updateByValue: function(entityName, condition, value, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    Condition: condition,
                    Value: value
                };

                BaseFunction.request('/api/entity/updateByValue', false, parameter, successHandle, errHandle);
            },
            _entityMetadatas: {},  //实体元素据属性集合，一旦查找过后，无需再查找
            getEntityMetadata: function(entityName, successHandle, errHandle){
                if(this._entityMetadatas.hasOwnProperty(entityName)){
                    if(successHandle){
                        successHandle(this._entityMetadatas[entityName]);
                    }
                }
                else{
                    var parameter = {
                        EntityName: entityName
                    };
                    BaseFunction.request('/api/entity/getEntityMetadata', false, parameter, function(entityMetadata){
                        this._entityMetadatas[entityName] = entityMetadata;
                        if(successHandle){
                            successHandle(entityMetadata);
                        }
                    }, errHandle);
                }
            },

            delete: function(entityName, record, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    Record: record
                };

                BaseFunction.request('/api/entity/delete', false, parameter, successHandle, errHandle);
            },
            deleteByCondition: function(entityName, condition, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    Condition: condition
                };

                BaseFunction.request('/api/entity/deleteByCondition', false, parameter, successHandle, errHandle);
            },
            deletes: function(entityName, conditions, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    Conditions: conditions
                };

                BaseFunction.request('/api/entity/deletes', false, parameter, successHandle, errHandle);
            },
            deleteById: function(entityName, id, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    ID: id
                };

                BaseFunction.request('/api/entity/deleteById', false, parameter, successHandle, errHandle);
            },
            deleteByIds: function(entityName, ids, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    IDs: ids
                };

                BaseFunction.request('/api/entity/deleteByIds', false, parameter, successHandle, errHandle);
            },

            find: function(entityName, condition, orderBy, populate, successHandle, errHandle) {
                if(orderBy == null){
                    orderBy = {};
                }
                if(populate == null){
                    populate = '';
                }

                var parameter = {
                    EntityName: entityName,
                    Condition: condition,
                    OrderBy: orderBy,
                    Populate: populate
                };

                BaseFunction.request('/api/entity/find', false, parameter, successHandle, errHandle);
            },
            findById: function(entityName, id, populate, successHandle, errHandle) {
                if(populate == null){
                    populate = '';
                }

                var parameter = {
                    EntityName: entityName,
                    ID: id,
                    Populate: populate
                };

                BaseFunction.request('/api/entity/findById', false, parameter, successHandle, errHandle);
            },
            findOne: function(entityName, condition, populate, successHandle, errHandle) {
                if(populate == null){
                    populate = '';
                }

                var parameter = {
                    EntityName: entityName,
                    Condition: condition,
                    Populate: populate
                };

                BaseFunction.request('/api/entity/findOne', false, parameter, successHandle, errHandle);
            },
            search: function(entityName, text, condition, limit, successHandle, errHandle) {
                var parameter = {
                    EntityName: entityName,
                    Text: text,
                    Condition: condition,
                    Limit: limit
                };

                BaseFunction.request('/api/entity/search', false, parameter, successHandle, errHandle);
            },
            distinct: function(entityName, field, condition) {
                var parameter = {
                    EntityName: entityName,
                    Field: field,
                    Condition: condition
                };

                var promise = new Promise(function(resolve, reject) {
                    BaseFunction.request('/api/entity/distinct', false, parameter, function(data){
                        resolve(data);
                    });
                });

                return promise;
            },
            aggregate: function(entityName, args) {
                var parameter = {
                    EntityName: entityName,
                    Arguments: args
                };

                var promise = new Promise(function(resolve, reject) {
                    BaseFunction.request('/api/entity/aggregate', false, parameter, function(data){
                        resolve(data);
                    });
                });

                return promise;
            },
            mapReduce: function(entityName, option, condition, orderBy) {
                var parameter = {
                    EntityName: entityName,
                    Option: option,
                    Condition: condition,
                    OrderBy: orderBy
                };

                var promise = new Promise(function(resolve, reject) {
                    BaseFunction.request('/api/entity/mapReduce', false, parameter, function(data){
                        resolve(data);
                    });
                });

                return promise;
            },

            //20160225模糊查询
            queryByKeyword:function(entityName, condition, orderBy, populate, successHandle, errHandle){
                if(orderBy == null){
                    orderBy = {};
                }
                if(populate == null){
                    populate = '';
                }

                var parameter = {
                    EntityName: entityName,
                    Condition: condition,
                    OrderBy: orderBy,
                    Populate: populate
                };

                BaseFunction.request('/api/entity/queryByKeyword', false, parameter, successHandle, errHandle);
            },

            pageQuery: function(entityName, condition, orderBy, populate, pageNum, pageSize, successHandle, errHandle){
                if(orderBy == null){
                    orderBy = {};
                }
                if(populate == null){
                    populate = '';
                }

                var parameter = {
                    EntityName: entityName,
                    Condition: condition,
                    OrderBy: orderBy,
                    Populate: populate,
                    PageNum : pageNum,
                    PageSize: pageSize
                };

                BaseFunction.request('/api/entity/pageQuery', false, parameter, successHandle, errHandle);
            },
            queryRecordCount: function(entityName, condition, successHandle, errHandle){
                var parameter = {
                    EntityName: entityName,
                    Condition: condition
                };

                BaseFunction.request('/api/entity/queryRecordCount', false, parameter, successHandle, errHandle);
            },
            findRecordIndex: function(entityName, id, condition, orderBy, successHandle, errHandle) {
                if(orderBy == null){
                    orderBy = {};
                }

                var parameter = {
                    EntityName: entityName,
                    ID: id,
                    Condition: condition,
                    OrderBy: orderBy
                };

                BaseFunction.request('/api/entity/findRecordIndex', false, parameter, successHandle, errHandle);
            }
        };
    }
]);

